package com.feini.ktv.svc.controller.om;


import com.feini.ktv.svc.entity.om.OmBox;
import com.feini.ktv.svc.entity.om.OmOrder;
import com.feini.ktv.svc.entity.om.OmOrderLine;
import com.feini.ktv.svc.entity.pm.PmBox;
import com.feini.ktv.svc.service.om.IOmBoxService;
import com.feini.ktv.svc.service.om.IOmOrderLineService;
import com.feini.ktv.svc.service.om.IOmOrderService;
import com.feini.ktv.svc.util.SysTimeUtil;
import com.podara.skeleton.controller.SkeletonController;
import com.podara.skeleton.log.annotation.LogLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jiangzr
 * @since 2021-04-14
 */
@LogLogger
@RestController
public class OmBoxController implements SkeletonController {
    @Autowired
    private IOmBoxService boxService;

    @Autowired
    private IOmOrderService orderService;

    @Autowired
    private IOmOrderLineService orderLineService;

    @PostMapping("/openBox")
    public boolean postOrderList(@RequestBody PmBox pmBox) {
        //订单明细存在一单多条业务-om_order_line
        LocalDateTime dateTime = LocalDateTime.now();
        String orderId = pushOrder(pmBox);
        long order_id = Long.valueOf(orderId);

        String orderLineId = pushOrderLine(order_id);

        OmBox box = new OmBox();
        box.setOrderId(order_id);
        box.setOrderLineId(Long.valueOf(orderLineId));
        box.setBoxId(Long.valueOf(pmBox.getBoxId()));
        box.setBoxName(pmBox.getBoxName());
        box.setBoxFee(String.valueOf(pmBox.getBoxPrice()));
        box.setAction("1");
        box.setDataStatus("0");
        box.setOrderStatus("0");
        box.setCreateOpId("jiang");
        box.setCreateOrgId("000000");
        box.setCreateDate(dateTime);
        box.setUpdateDate(dateTime);
        box.setRemark("测试");
        return boxService.save(box);
    }

    public String pushOrder(PmBox pmBox) {
        LocalDateTime dateTime = LocalDateTime.now();
        OmOrder order = new OmOrder();
        String orderNum = SysTimeUtil.getSysDateYYYYMMDDHHMMSS();
        if (!Objects.isNull(pmBox)) {
            order.setOrderId(Long.valueOf(orderNum));
            order.setBusiCode("101");
            order.setCustId(10000001L);
            order.setPartyName("jzr");
            order.setPartyPhone(13974970860L);
            order.setInModeCode("1");
            order.setIntactContent("om_order_h");
            order.setIntactId(101L);
            order.setDataStatus("0");
            order.setOrderStatus("0");
            order.setCreateOpId("jiang");
            order.setCreateOrgId("000000");
            order.setCreateDate(dateTime);
            order.setValidDate(dateTime);
            order.setExpireDate(dateTime.withYear(2050).withMonth(12));
            order.setRemark("测试");
            this.orderService.save(order);
        }
        return orderNum;
    }

    public String pushOrderLine(long orderId) {
        LocalDateTime dateTime = LocalDateTime.now();
        OmOrderLine orderLine = new OmOrderLine();
        String orderLineId = SysTimeUtil.getSysDateYYYYMMDDHHMMSS();
        orderLine.setOrderId(orderId);
        orderLine.setIntactId(101L);
        orderLine.setOrderLineId(Long.parseLong(orderLineId));
        orderLine.setIntactItemContent("om_order_line_h");
        orderLine.setAccessNum("13974970860");
        orderLine.setDataStatus("0");
        orderLine.setOrderStatus("0");
        orderLine.setCreateOpId("jiang");
        orderLine.setCreateOrgId("000000");
        orderLine.setCreateDate(dateTime);
        orderLine.setValidDate(dateTime);
        orderLine.setExpireDate(dateTime.withYear(2050).withMonth(12));
        orderLine.setRemark("测试");
        this.orderLineService.save(orderLine);
        return orderLineId;
    }

}